package com.ruge.system.controller;

import com.ruge.common.util.RestResponse;
import com.ruge.common.util.ShiroUtil;
import com.ruge.system.pojo.entity.SysUser;
import com.ruge.system.service.SysUserService;
import com.ruge.system.service.impl.FastDFSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author ：爱丽丝、如歌
 * @time ：2020/3/21  9:29
 * 描述 ：
 */
@RestController
@RequestMapping("upload")
@Api(value = "文件上传 controller", tags = {"文件上传 接口"})
public class SysUploadController {
    @Resource
    private FastDFSClient fastDFSClient;
    @Resource
    private SysUserService sysUserService;

    @PostMapping("headImage")
    @ApiOperation(value = "用户头像上传")
    public RestResponse headImage(@RequestParam("file") MultipartFile file) {
        String filePath = null;
        SysUser user = null;
        try {
            filePath = fastDFSClient.uploadFile(file);
            user = sysUserService.findByLoginName(ShiroUtil.getActiveUser().getUser().getLoginName());
            user.setHeadImage(filePath);
            sysUserService.save(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return RestResponse.success().setData(user);
    }

}
